package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.ChatMessageBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.util.SPUtils;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/8/6 0006.
 */

public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int MESSAGE_RECEIVE = 0;   // 接收到的消息
    private static final int MESSAGE_SEND = 1;      // 发送的消息
    private static final int MESSAGE_GOOD = 2;      // 发送的消息

    private OnItemClickListener mListener;
    private List<ChatMessageBean> mList;
    private Context mContext;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public ChatMessageAdapter(List<ChatMessageBean> list, Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        ChatMessageBean chatMessage = mList.get(position);
        if (!TextUtils.isEmpty(chatMessage.getMessage()) && chatMessage.getMessage().startsWith("#3A2B1C#")) {
            return MESSAGE_GOOD;
        } else {
            if (chatMessage.getUid().equals(UserInfoBean.getUid(mContext))) {
                return MESSAGE_SEND;
            } else {
                return MESSAGE_RECEIVE;
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case MESSAGE_SEND:
                LayoutInflater sendInflater = LayoutInflater.from(mContext);
                View viewSend = sendInflater.inflate(R.layout.item_message_send, parent, false);
                return new ViewHolder(viewSend);
            case MESSAGE_RECEIVE:
                LayoutInflater receiveInflater = LayoutInflater.from(mContext);
                View viewReceive = receiveInflater.inflate(R.layout.item_message_receive, parent, false);
                return new ViewHolder(viewReceive);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            final ChatMessageBean bean = mList.get(position);
            switch (bean.getType()) {
                case "TXT":
                    ((ViewHolder) holder).message.setText(mList.get(position).getMessage());
                    ((ViewHolder) holder).message.setVisibility(View.VISIBLE);
                    ((ViewHolder) holder).img.setVisibility(View.GONE);
                    break;
                case "IMAGE":
                    RequestOptions requestOptions = new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .centerCrop()
                            .override(350, 350);
                    Glide.with(mContext.getApplicationContext())
                            .load(TextUtils.isEmpty(
                                    bean.getMessage()) ?
                                    R.mipmap.default_avatar :
                                    bean.getMessage())
                            .apply(requestOptions)
                            .into(((ViewHolder) holder).img);
                    ((ViewHolder) holder).message.setVisibility(View.GONE);
                    ((ViewHolder) holder).img.setVisibility(View.VISIBLE);
                    break;
                case "LOCATION":
                    ((ViewHolder) holder).message.setText(mList.get(position).getMessage());
                    ((ViewHolder) holder).message.setVisibility(View.VISIBLE);
                    ((ViewHolder) holder).img.setVisibility(View.GONE);
                    break;
                case "VOICE":
                    ((ViewHolder) holder).message.setText(mList.get(position).getMessage());
                    ((ViewHolder) holder).message.setVisibility(View.VISIBLE);
                    ((ViewHolder) holder).img.setVisibility(View.GONE);
                    break;
                case "VIDEO":
                    ((ViewHolder) holder).message.setText(mList.get(position).getMessage());
                    ((ViewHolder) holder).message.setVisibility(View.VISIBLE);
                    ((ViewHolder) holder).img.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
            ((ViewHolder) holder).img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.previewImage(bean.getMessage());
                }
            });
            String avatar = SPUtils.getString(mContext, "avatar" + bean.getUid(), bean.getUid());
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(150, 150);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(avatar) ? R.mipmap.default_avatar : HttpRequestConstant.USER_PIC_HEAD + avatar)
                    .apply(requestOptions)
                    .into(((ViewHolder) holder).avatar);
            ((ViewHolder) holder).avatar.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mListener.userInfo(bean.getUid());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null && mList.size() > 0) {
            return mList.size();
        } else {
            return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public CircleImageView avatar;
        public TextView message;
        public ImageView img;

        public ViewHolder(View view) {
            super(view);
            avatar = (CircleImageView) view.findViewById(R.id.avatar);
            message = (TextView) view.findViewById(R.id.message);
            img = (ImageView) view.findViewById(R.id.img_message);
        }
    }

    public interface OnItemClickListener{
        void previewImage(String imagePath);
        void userInfo(String uid);
    }
}

